Skip to content

Fix repr() of the smallest subnormal floats not being the shortest string - #1351

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/float-repr-subnormal
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/float-repr-subnormal

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Part of #1291 (item 6).

FloatFormatter.reprDigits takes its digits from Double.toString, which never prints fewer than two digits and so picks the two-digit decimal closest to the value even when a one-digit decimal round-trips. CPython's float_repr prints the shortest string that round-trips.

AS-IS

>>> repr(5e-324)
'4.9e-324'
>>> repr(1e-323)
'9.9e-324'
>>> repr(5e-323)
'4.9e-323'

TO-BE

>>> repr(5e-324)
'5e-324'
>>> repr(1e-323)
'1e-323'
>>> repr(5e-323)
'5e-323'

(matching CPython)

Changes

  • In reprDigits, for a subnormal value use the nearest one-digit decimal when it converts back to the same double.
  • Add a test to ReprTests in tests/test_float.py.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest on test_float.py (the new test fails on the unpatched build and passes with the fix) and the tagged test_float (49 passed). mx python-checkcopyrights, mx eclipseformat and mx checkstyle are clean.

repr(5e-324) was '4.9e-324': Double.toString never prints fewer than two
digits, so it picks the two-digit decimal closest to the value even when
a one-digit decimal round-trips. Use the one-digit decimal for a
subnormal when it converts back to the same double.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant